<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>拆分</title>
    <script type="text/javascript">
      Ext.onReady(function(){
        //是否临时保存
        var temporary = Ext.create('Ext.form.field.Hidden', {
          name: 'temporary',
          value: 0
        });
        
        var separateOrderId = Ext.create('Ext.form.field.Hidden', {
          name: 'id'
        });
        
        //所有仓库
        var fromWarehouse = Ext.create('Ext.form.ComboBox', {
          store: warehouseStore,
          fieldLabel: '选择仓库',
          allowBlank: false,
          blankText: "仓库必须选择",
          emptyText: '请选择...',
          valueField: 'id',
          displayField: 'name',
          name: 'warehouse_id',
          loadingText: '搜索中...',
          anchor: '100%',
          //pageSize: 10,
          minChars: 0,
          triggerAction:'all'
        });        

        //将要拆分的货品的Combobox
       //产品
        var productCBB = Ext.create('Ext.form.ComboBox', {
          store: productSuitStore,
          fieldLabel: '产品',
          emptyText: '请选择...',
          valueField: 'id',
          displayField: 'name',
          name: 'product_id',
          loadingText: '搜索中...',
          anchor: '100%',
          pageSize: 10,
          minChars: 0,
          listConfig: {
            loadingText: 'Searching...',
            getInnerTpl: function() {
              return '<div style="float:left; width:200px" data-qtip="{name}">{name:ellipsis(18)}</div><div style="float:left; width:110px">{number}</div>{is_suit}';
            }
          }
        });

        productCBB.on('select', function(combo, record, index) {
          number.setValue(record[0].get('number'));
          unit.setValue(record[0].get('unit'));
          spec.setValue(record[0].get('spec'));
          subProductDS.getProxy().extraParams.id = record[0].get('id');
          subProductDS.load();
        });

        //要拆分的数量
        var quantity = Ext.create('Ext.form.field.Number', {
          fieldLabel: '拆分数量',
          name: 'quantity',
          blankText: "数量不能为空",
          anchor: '100%',
          allowBlank: false,
          allowDecimals: false,
          minValue: 0
        })

        //编号
        var number = Ext.create('Ext.form.field.Text', {
          fieldLabel: '编号',
          readOnly: true,
          name: 'number',
          anchor: '90%'
        });

        //规格
        var spec = Ext.create('Ext.form.field.Text', {
          fieldLabel: '规格',
          readOnly: true,
          name: 'spec',
          anchor: '90%'
        });

        //单位
        var unit = Ext.create('Ext.form.field.Text', {
          fieldLabel: '单位',
          readOnly: true,
          name: 'unit',
          anchor: '90%'
        });
        //单价
        var unitPrice = Ext.create('Ext.form.field.Text', {
          fieldLabel: '单价',
          readOnly: true,
          name: 'unit_price',
          anchor: '90%'
        });

        var description = Ext.create('Ext.form.field.Text', {
          fieldLabel: '备注',
          name: 'description',
          anchor: '90%'
        });
        //生成要拆分的货品内包含的所有其它产品的Grid的CM
        //将要拆分的货品Grid Store
        var subProductDS = Ext.create('Ext.data.Store', {
          autoDestroy : true,
          model : 'SubProductModel',
          proxy : {
            type : 'ajax',
            url: '/product/getAllComponentsById',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          }
        });

        //拆分后的产品的Grid
        var subProductGrid = Ext.create('Ext.grid.Panel', {
          store: subProductDS,
          columns : [{              
              header: 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '产品名称',
              dataIndex: 'name',
              width: 230
            },{
              header: '产品编号',
              dataIndex: 'number',
              width: 130
            },{
              header: '规格',
              dataIndex: 'spec',
              width: 50
            },{
              header: '单位',
              dataIndex: 'unit',
              width: 50
            },{
              header: '数量',
              align: 'right',
              dataIndex: 'quantity',
              width: 50
            },{
              header: '金额',
              dataIndex: 'amount',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 60
            },{
              header: '套装或单品',
              dataIndex: 'is_suit',
              width: 70
            }],
          selModel :Ext.create('Ext.selection.CheckboxModel'),
          width: 780,
          height: 250,
          //frame: true,
          title: '产品拆分列表'
          //frame: true,
          //plugins: checkColumn,
        });

        //暂时保存按钮
        var submitButton = Ext.create('Ext.Button', {
          text: '提交',
          tooltip: '提交拆分单。',
          iconCls: 'icon-save',
          handler: function(){
            submitButton.setDisabled(true);
            if (separateFormPanel.form.isValid()) {
              if (quantity.getValue() == 0){
                Ext.MessageBox.alert('提示信息', '拆分数量不能等于0');
                submitButton.setDisabled(false);
                return;
              }
              separateFormPanel.form.submit({
                success: function(result, resp){
                  if (resp.result.info.indexOf("成功") >= 0) {
                    editSeparateWin.destroy();
                  } else {
                    Ext.MessageBox.show({
                      title: '消息',
                      msg: resp.result.info,
                      buttons: Ext.MessageBox.OK,
                      icon: Ext.MessageBox.WARNING
                    });                    
                  }
                  submitButton.enable();
                },
                failure: function(result, request){
                  submitButton.enable();
                  Ext.MessageBox.show({
                    title: '消息',
                    msg: "通讯失败，请从新操作",
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.WARNING
                  });
                }
              });
            }
            else {
              submitButton.enable();
              Ext.MessageBox.show({
                title: '消息',
                msg: "请输入必输选项！",
                buttons: Ext.MessageBox.OK,
                icon: Ext.MessageBox.WARNING
              });
            }
          }
        });

        //临时保存按钮
        var tempSaveButton = Ext.create('Ext.Button', {
          text: '暂存',
          tooltip: '暂时保存销售订单。',
          iconCls: 'temp-save',
          handler: function(){
            //首先把临时变量设置为1
            temporary.setValue(1);

            if (separateFormPanel.form.isValid()){
              if (quantity.getValue() == 0){
                Ext.MessageBox.alert('提示信息', '拆分数量不能等于0');
                return;
              }
              tempSaveButton.setDisabled(true);
              separateFormPanel.form.submit({
                success: function(result, resp){
                  if (resp.result.info.indexOf("成功") >= 0) {
                    editSeparateWin.destroy();
                  } else {
                    Ext.MessageBox.show({
                      title: '消息',
                      msg: resp.result.info,
                      buttons: Ext.MessageBox.OK,
                      icon: Ext.MessageBox.WARNING
                    });                    
                  }
                  tempSaveButton.enable();
                },
                failure: function(result, request){
                  submitButton.enable();
                  Ext.MessageBox.show({
                    title: '消息',
                    msg: "通讯失败，请从新操作",
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.WARNING
                  });
                }
              });
            }
          }
        });
        
        //暂时保存按钮
        var closeButton = Ext.create('Ext.Button', {
          text: '关闭',
          tooltip: '关闭当前窗口，并且当前编辑的信息不能保存。',
          iconCls: 'exit',
          handler: function(){
            editSeparateWin.destroy();
          }
        });
        
        var separateFormPanel = Ext.create('Ext.form.Panel', {
          fieldDefaults: {
            labelWidth: 55,
            labelAlign: 'right'
          },
          width: '800',
          frame : true,
          url: '/separate_order/update',
          method: 'GET',
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            }, {
              name: 'product_id'
            }, {
              name: 'quantity'
            }, {
              name: 'description'
            }, {
              name: 'number',
              mapping:'product.number'
            }, {
              name: 'spec',
              mapping:'product.spec'
            }, {
              name: 'unit_price',
              mapping:'product.unit_price'
            }, {
              name: 'unit',
              mapping:'product.unit'
            }, {
              name: 'warehouse_id'
            }]),
          //defaultType: 'textfield',
          items: [{
              xtype: 'fieldset',
              title: '拆分产品必选项',
              layout: 'column',
              border: true,
              items: [{
                  columnWidth: .4,
                  layout: 'anchor',
                  xtype: 'container',
                  items: [fromWarehouse]
                }, {
                  columnWidth: .4,
                  layout: 'anchor',
                  xtype: 'container',
                  items: [productCBB]
                }, {
                  columnWidth: .2,
                  layout: 'anchor',
                  xtype: 'container',
                  items: [quantity]
                }]
            },{
              xtype: 'fieldset',
              title: '将要拆分产品明细',
              layout: 'column',
              border: true,
              items: [{
                  columnWidth: .25,
                  layout: 'anchor',
                  xtype: 'container',
                  items: [number]
                }, {
                  columnWidth: .25,
                  layout: 'anchor',
                  xtype: 'container',
                  items: [spec]
                }, {
                  columnWidth: .25,
                  layout: 'anchor',
                  xtype: 'container',
                  items: [unit]
                }, {
                  columnWidth: .25,
                  layout: 'anchor',
                  xtype: 'container',
                  items: [unitPrice]
                }]
            },{
              layout: 'column',
              xtype: 'container',
              items: [{
                  columnWidth: 1,
                  layout: 'anchor',
                  xtype: 'container',
                  items: [subProductGrid]
                }]
            },description,separateOrderId,temporary],
          buttons: [submitButton,tempSaveButton,closeButton]
        });

        warehouseStore.load({callback: function(record, options, success){
            if(success){
              productStore.load({callback: function(record, options, success){
                  if(success){
                    separateFormPanel.form.load({
                      url: '/separate_order/getSeparateOrderById?id=' + <%=params[:id]%>,
                      method : 'GET',
                      waitMsg: '正在载入数据...',
                      success: function(form, action){
                        subProductDS.getProxy().extraParams.id = productCBB.getValue() ;
                        subProductDS.load();
                      },
                      failure: function(form, action){
                        Ext.MessageBox.alert('提示信息', '信息加载失败');
                      }
                    });
                  }else{
                    Ext.MessageBox.alert('操作','失败！');
                  }
                }});
            }else{
              Ext.MessageBox.alert('操作','失败！');
            }
          }});
        separateFormPanel.render('separate_form');
      })
    </script>
  </head>
  <body>
    <div id="separate_form"></div>
  </body>
</html>
